package server;

import java.io.Serializable;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@SuppressWarnings("serial")
public class Email extends Thread implements Serializable {

	private static final String USER_NAME = "ss11.security";

	private static final String PASSWORD = "Ss123456";

	private MimeMessage mimeMessage;

	private Address senderEmail;

	private static Properties getProperties() {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.starttls.enable", "true");
		return props;
	}

	public Email(String senderEmailAddress, String senderName, String subject,
			String message, String recipentEmailAddress) {
		this.mimeMessage = new MimeMessage(Session.getInstance(getProperties(),
				null));
		try {
			this.mimeMessage.setContent("Mail content", "text/html");
			this.mimeMessage.setFrom(senderEmail);
			this.mimeMessage.setSubject(subject);
			this.mimeMessage.setText(message);
			Address recipent = new InternetAddress(recipentEmailAddress);
			this.mimeMessage.addRecipient(Message.RecipientType.TO, recipent);
			this.senderEmail = new InternetAddress(senderEmailAddress,
					senderName);
			this.mimeMessage.setSender(senderEmail);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void send() {
		this.start();

	}

	public void run() {
		Transport t;
		try {
			Session session = Session.getInstance(getProperties(), null);
			t = session.getTransport("smtp");
			t.connect(USER_NAME, PASSWORD);
			t
					.sendMessage(this.mimeMessage, this.mimeMessage
							.getAllRecipients());
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
}
